package controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.FundDAO;
import model.Model;
import model.TransactionDAO;

import org.mybeans.dao.DAOException;

import util.Bean2XML;
import databean.TransactionBean;

public class GetTransactionAction extends Action {
	private TransactionDAO transactionDAO;
    private FundDAO fundDAO;
	public GetTransactionAction(Model model) {
		this.transactionDAO = model.getTransactionDAO();
		this.fundDAO=model.getFundDAO();
	}

	@Override
	public String getName() {
		return "get-transaction.do";
	}

	@Override
	public String perform(HttpServletRequest request,
			HttpServletResponse response) {
		String rs="";
		String cid=request.getParameter("customerID");
		
		//case get all record
		if(cid==null||cid.length()==0){
		//TODO for employee	
		}
		
		//case get record for one customer
		else{
		long cid_num=Long.parseLong(cid);
		try {
			TransactionBean[] trans=transactionDAO.lookupByCustomerID(cid_num);
			rs=Bean2XML.Transactions(trans,fundDAO);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		
		
		response.setContentType("application/xml"); 
		response.setHeader("Content-disposition","attachment; filename=transactions.xml"); 
		try {
			PrintWriter out;
			out = response.getWriter();
			out.print(rs);
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "empty.jsp";
	}

}
